package mortal.game.control
{
    import modules.*;
    import mortal.game.*;
    import mortal.game.events.*;
    import mortal.game.model.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.rules.*;
    import mortal.game.scene.ai.*;
    import mortal.game.scene.map.*;
    import mortal.game.view.avatar.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class GuideDrugController extends Controller
    {
        private var _sceneNotInit:Boolean;
        private var _drugType:int;
        private var drugTarget:TaskTargetInfo;

        public function GuideDrugController()
        {
            
        }

        private function onGotoBuyDrug(event:DataEvent) : void
        {
            AIManager.onAutoPathAIControl(event.data as TaskTargetInfo);
            
        }

        private function onFlyToBuyDrug(event:DataEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.ConveyToReq, event.data));
            
        }

        private function onGuideDrugBuyClose(event:DataEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.GuideDrugBuyClose));
            
        }

        private function onRoleDrugNotEnough(event:DataEvent) : void
        {
            this._drugType = int(event.data);
            if (!Game.scene.isInitialize)
            {
                this._sceneNotInit = true;
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
                
            }
            this.guideDrug(this._drugType);
            
        }

        private function onSceneUpdateHandler(event:DataEvent) : void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            if (this._sceneNotInit)
            {
                this.guideDrug(this._drugType);
                this._sceneNotInit = false;
            }
            
        }

        private function guideDrug(param1:int) : void
        {
            var _loc_2:int = 0;
            var _loc_3:SceneInfo = null;
            var _loc_4:NPCInfo = null;
            if (param1 == RoleDrugEnoughType.allEnough)
            {
                if (_view && !_view.isHide)
                {
                    _view.hide();
                }
            }
            else if (view.isHide)
            {
                if (view.isHide)
                {
                    _loc_2 = cache.scene.getDragNPC();
                    if (_loc_2 != -1)
                    {
                        _loc_3 = SceneConfig.instance.getSceneInfoByNpcId(_loc_2);
                        _loc_4 = _loc_3.getNpcInfo(_loc_2);
                        if (!this.drugTarget)
                        {
                            this.drugTarget = new TaskTargetInfo();
                        }
                        this.drugTarget.id = _loc_2;
                        this.drugTarget.mapId = _loc_3.sMapDefine.mapId;
                        this.drugTarget.x = _loc_4.snpc.point.x;
                        this.drugTarget.y = _loc_4.snpc.point.y;
                        this.drugTarget.name = _loc_4.tnpc.name;
                        this.drugTarget.mapName = _loc_3.sMapDefine.name;
                        this.drugTarget.targetType = EntityType.NPC;
                        (view as GuideDrugModule).updateData(this.drugTarget, param1);
                        view.show();
                    }
                }
            }
            
        }

        override protected function initServer() : void
        {
            super.initServer();
            Dispatcher.addEventListener(EventName.RoleDrugNotEnough, this.onRoleDrugNotEnough);
            
        }

        override protected function initView() : IView
        {
            var _loc_1:* = new GuideDrugModule();
            _loc_1.addEventListener(EventName.AutoPathBuyDrug, this.onGotoBuyDrug);
            _loc_1.addEventListener(EventName.FlyToBuyDrug, this.onFlyToBuyDrug);
            _loc_1.addEventListener(EventName.GuideDrugBuyClose, this.onGuideDrugBuyClose);
            return _loc_1;
        }

    }
}
